import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.SocketHandler;

public class Test {

    public static void main(String[] args) {
        try {
            Socket socket = new Socket("192.168.3.164",6588);
            ExecutorService pool = Executors.newFixedThreadPool(2);
            pool.execute(new chatIn(socket));
            pool.execute(new chatOut(socket));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    static class chatIn implements Runnable{

        private  Socket socket;

        public chatIn(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (true) {
                    String s = br.readLine();

                    if (s == null){
                        break;
                    }
                    System.out.println("服务端说:"+s);
                }
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    static class chatOut implements Runnable{
        private  Socket socket;

        public chatOut(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {

            try (
                  OutputStream os = socket.getOutputStream();
                  PrintWriter p = new PrintWriter(new OutputStreamWriter(os),true)){
               while (true){
                   String input = InputUtils.Input("请输入想发送的信息:");
                   p.println(input);
               }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
